package com.easyexcle.demo.easyExcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSON;
import com.easyexcle.demo.easyExcel.style.ExcelWidthStyleStrategy;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * @Author: lkz
 * @Title: Test
 * @Description: TODO
 * @Date: 2022/12/7 10:58
 */

public class Test {

    @GetMapping("/importTemplate")
    public void  importTemplate(HttpServletResponse response) throws IOException {


        List<List<String>> headList=new ArrayList<>();
        String[] headData = this.headData();
        for(String head:headData){
            List<String> keyList=new ArrayList<>();
            keyList.add(head);
            headList.add(keyList);
        }
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("实验室检查项模板", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream()).head(headList)
                    .registerWriteHandler(new ExcelWidthStyleStrategy())//设置宽度
                    // .registerWriteHandler(new TextCellWriteHandlerImpl())
                    .registerWriteHandler(new HorizontalCellStyleStrategy(this.getHeadStyle(),this.getContentStyle()))
                    .useDefaultStyle(false)//取消默认样式 图二 t 默认使用样式 表头有阴影
                    .autoCloseStream(Boolean.FALSE).sheet("模板")
                    .doWrite(null);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<String, String>();
            map.put("code", "9999");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }

    }

    private String[] headData(){
        String [] head={"中心编号","实验室编号","表单模板key","检查项名称","检查项编码","性别","年龄下限值","年龄上限值","检查项下限值",
                "检查项上限值","检查项单位","生效时间","失效时间"};
        return head;
    }

    /**
     * 标题样式
     * @return
     */
    public static WriteCellStyle getHeadStyle(){
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
//        headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

        // 字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontName("宋体");//设置字体名字
        headWriteFont.setFontHeightInPoints((short)10);//设置字体大小
        headWriteFont.setBold(false);//字体加粗
        headWriteCellStyle.setWriteFont(headWriteFont); //在样式用应用设置的字体;

        // 样式
//        headWriteCellStyle.setBorderBottom(BorderStyle.THIN);//设置底边框;
//        headWriteCellStyle.setBottomBorderColor((short) 0);//设置底边框颜色;
//        headWriteCellStyle.setBorderLeft(BorderStyle.THIN);  //设置左边框;
//        headWriteCellStyle.setLeftBorderColor((short) 0);//设置左边框颜色;
//        headWriteCellStyle.setBorderRight(BorderStyle.THIN);//设置右边框;
//        headWriteCellStyle.setRightBorderColor((short) 0);//设置右边框颜色;
//        headWriteCellStyle.setBorderTop(BorderStyle.THIN);//设置顶边框;
//        headWriteCellStyle.setTopBorderColor((short) 0); //设置顶边框颜色;
//
//        headWriteCellStyle.setWrapped(true);  //设置自动换行;

        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//设置水平对齐的样式为居中对齐;
        // headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);  //设置垂直对齐的样式为居中对齐;
        headWriteCellStyle.setShrinkToFit(true);//设置文本收缩至合适

        return headWriteCellStyle;
    }


    /**
     * 内容样式
     * @return
     */
    public static WriteCellStyle getContentStyle(){
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();

        // 背景绿色
        // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
//        contentWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
//        contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

        // 设置字体
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontHeightInPoints((short) 9);//设置字体大小
        contentWriteFont.setFontName("宋体"); //设置字体名字
        contentWriteCellStyle.setWriteFont(contentWriteFont);//在样式用应用设置的字体;

        //设置样式;
//        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);//设置底边框;
//        contentWriteCellStyle.setBottomBorderColor((short) 0);//设置底边框颜色;
//        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);  //设置左边框;
//        contentWriteCellStyle.setLeftBorderColor((short) 0);//设置左边框颜色;
//        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);//设置右边框;
//        contentWriteCellStyle.setRightBorderColor((short) 0);//设置右边框颜色;
//        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);//设置顶边框;
//        contentWriteCellStyle.setTopBorderColor((short) 0); ///设置顶边框颜色;

        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);// 水平居中
        // contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
        // contentWriteCellStyle.setWrapped(true); //设置自动换行;

        contentWriteCellStyle.setDataFormat((short)49);//设置单元格格式是：文本格式，方式长数字文本科学计数法

        contentWriteCellStyle.setShrinkToFit(true);//设置文本收缩至合适

        return contentWriteCellStyle;
    }


    public static void main(String[] args) {
        List<Integer> gcList=new ArrayList<>();
        gcList.add(1);
        gcList.add(2);
        gcList.add(3);
        int[] gcRateArr = new int[gcList.size()];
        for(int i=0;i<gcList.size();i++){
            Integer integer = gcList.get(i);

            gcRateArr[i] = integer;
        }
        System.out.println(gcRateArr);

        int minBlockSize = Arrays.stream(gcRateArr).sum();
        System.out.println(minBlockSize);
    }


}
